<?php
namespace app\client\controller;

use think\Url;
use think\Loader;
use think\Session;
use think\Request;
use think\Controller;
use app\common\trilateral\Classify as ClassifyAlias;
use app\client\model\ShopGoodsType as ShopGoodsTypeModel;

class ShopGoodsType extends Controller
{

    /**
     * 商品分类列表
     * @param ShopGoodsTypeModel $shopGoodsType
     * @return mixed
     */
    public function index(ShopGoodsTypeModel $shopGoodsType)
    {
        $data = $shopGoodsType->where(['member_id' => Session::get('shop_id')])->order(['sort' => 'asc', 'parenTid' => 'asc', 'id' => 'asc'])->select()->toArray();


        return $this->fetch('', [
            'mate_title' => '商品分类列表',
            'data'       => ClassifyAlias::dimension($data)
        ]);
    }


    /**
     * 新增商品分类
     * @param Request $request
     * @param ShopGoodsTypeModel $shopGoodsType
     * @return mixed
     */
    public function create(Request $request, ShopGoodsTypeModel $shopGoodsType)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

            // 验证
            $validate = Loader::validate('ShopGoodsType');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $shopGoodsType->allowField(true)->save($data);

            if ($dispose) {
                win('操作成功', Url::build('ShopGoodsType/index'));
            }

            fail('操作失败');


        }

        $data = $shopGoodsType->where(['member_id' => Session::get('shop_id')])->order(['parenTid' => 'asc', 'id' => 'asc'])->select()->toArray();


        return $this->fetch('', [
            "mate_title" => '新增商品分类',
            'data'       => ClassifyAlias::dimension($data)

        ]);

    }


    /**
     * 删除店铺商品分类
     * @param $id
     * @param ShopGoodsTypeModel $shopGoodsType
     */
    public function destroy($id, ShopGoodsTypeModel $shopGoodsType)
    {

        if ($shopGoodsType->amputate($id)) {

            win('操作成功', Url::build('ShopGoodsType/index'));

        }

        fail('操作失败');

    }


    /**
     * 删除全部店铺商品分类
     * @param $id
     * @param ShopGoodsTypeModel $shopGoodsType
     */
    public function destroyAll($id, ShopGoodsTypeModel $shopGoodsType)
    {

        if ($shopGoodsType->amputateAll($id)) {
            win('操作成功', Url::build('ShopGoodsType/index'));
        }

        fail('操作失败');

    }


    /**
     * 编辑店铺商品分类
     * @param $id
     * @param Request $request
     * @param ShopGoodsTypeModel $shopGoodsType
     * @return mixed
     */
    public function update($id, Request $request, ShopGoodsTypeModel $shopGoodsType)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

            // 验证
            $validate = Loader::validate('ShopGoodsType');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $shopGoodsType->allowField(true)->save($data, ['id' => $id]);

            if ($dispose) {
                win('操作成功', Url::build('ShopGoodsType/index'));
            }

            fail('操作失败');

        }

        $data = $shopGoodsType->where(['member_id' => Session::get('shop_id')])->order(['parenTid' => 'asc', 'id' => 'asc'])->select()->toArray();
        $item = $shopGoodsType::get($id);


        return $this->fetch('create', [
            "mate_title" => '编辑商品分类',
            'data'       => ClassifyAlias::dimension($data),
            'item'       => $item

        ]);
    }


    /**
     * 更改店铺商品分类
     * @param ShopGoodsTypeModel $shopGoodsType
     * @return bool|int
     */
    public function auditing(ShopGoodsTypeModel $shopGoodsType)
    {
        $id = Request::instance()->param('id');

        $status = $shopGoodsType->where('id', $id)->value('status') == '1' ? 2 : 1;

        if ($shopGoodsType->where('id', $id)->update(['status' => $status])) {
            return $status;
        }
        return false;
    }


    /**
     * 更改店铺商品分类顺序
     * @param ShopGoodsTypeModel $shopGoodsType
     */
    public function sorting(ShopGoodsTypeModel $shopGoodsType)
    {

        $id = Request::instance()->param('orderId/a');
        $sort = Request::instance()->param('sort/a');

        foreach ($id as $key => $value) {
            $shopGoodsType->where('id', $value)->update(['sort' => $sort[$key]]);
        }

        win('操作成功', Url::build('ShopGoodsType/index'));
    }
}
